<template>
  <div class="app-container">
    <span>搜索内容：</span>
    <el-input v-model="keyword" style="width: 150px; padding: 10px" placeholder="请输入内容" />
    <el-button type="primary" round @click="handleSearch">搜索</el-button>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="用户" width="150">
        <template slot-scope="scope">
          {{ handleName(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="150">
        <template slot-scope="scope">
          {{ scope.row.phone? scope.row.phone: '-' }}
        </template>
      </el-table-column>
      <el-table-column label="邮箱" width="150">
        <template slot-scope="scope">
          {{ scope.row.email? scope.row.email: '-' }}
        </template>
      </el-table-column>
      <el-table-column label="最后登录时间" width="150">
        <template slot-scope="scope">
          {{ scope.row.lastLogin? scope.row.lastLogin: '-' }}
        </template>
      </el-table-column>
      <el-table-column label="登录次数" width="150">
        <template slot-scope="scope">
          {{ scope.row.loginsCount? scope.row.loginsCount: '0' }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createdAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click=" handelDelete(scope.row.id)"></el-button>

          <el-button
            size="mini"
            type="info"
            circle
           >禁用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData"
    />
  </div>
</template>
<script>
import { getManagementClient } from '@/utils/authing-util'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      keyword: '',
      page: 1,
      size: 10,
      total: 0
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    handleSearch() {
      this.listLoading = true
      getManagementClient().users.search(this.keyword).then(res => {
        this.listLoading = false
        this.list = res.list
        this.total = res.totalCount
      })
    },
    handelDelete(id) {
      this.$confirm('此操作将删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        getManagementClient().users.delete(id).then(res => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleName(val) {
      if (val.nickname) {
        return val.nickname
      } else if (val.name) {
        return val.name
      } else if (val.username) {
        return val.username
      } else if (val.email) {
        return val.email
      }
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      getManagementClient().users.list(this.page, this.size).then(res => {
        this.listLoading = false
        this.list = res.list
        this.total = res.totalCount
      })
    }
  }
}
</script>
